<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="详情" @ok="handleSubmit">
    <BasicForm @register="registerForm">
      <template #detailMessage="{ model, field }">
        <CodeEditor v-model:value="model[field]" mode="application/json" :readonly="true" />
      </template>
    </BasicForm>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, toRaw } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from './log.data';
  import { CodeEditor } from '/@/components/CodeEditor';

  export default defineComponent({
    name: 'DetailModal',
    components: { BasicModal, BasicForm, CodeEditor },
    setup(_) {
      const [registerForm, { resetFields, setFieldsValue }] = useForm({
        labelWidth: 100,
        schemas: formSchema,
        showActionButtonGroup: false,
        disabled: true,
      });

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async ({ record }) => {
        resetFields();
        setFieldsValue(toRaw(record));
        setModalProps({ confirmLoading: false, maskClosable: false });
      });

      async function handleSubmit() {
        try {
          closeModal();
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return { registerModal, registerForm, handleSubmit };
    },
  });
</script>
<style lang="less">
  .ant-form-item-control-input-content {
    #detailMessage {
      overflow-y: auto !important;
    }
  }
</style>
